package com.wdzl.gui;


import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.wdzl.dao.SchoolDao;
import com.wdzl.dao.StudentDao;
import com.wdzl.entity.Student;

public class AddForm extends JFrame {

	private ButtonGroup buttonGroup = new ButtonGroup();
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JTextField jNameTextField = null;
	private JButton jButton = null;
	private JLabel jUpassLabel1 = null;
	private JTextField jAgeTextfield = null;
	private JLabel jEmailLabel1 = null;
	private JTextField jEmailTextField1 = null;
	private JLabel jPhoneLabel1 = null;
	private JTextField jPhoneTextField1 = null;
	private JLabel jAddressLabel1 = null;
	private JComboBox jAddressComboBox = null;
	private JButton jButton1 = null;
	private JRadioButton manRadioButton;
	private JRadioButton womanRadioButton;
	/**
	 * This method initializes jTextField
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getJNameTextField() {
		if (jNameTextField == null) {
			jNameTextField = new JTextField();
			jNameTextField.setBounds(new Rectangle(131, 5, 101, 26));
		}
		return jNameTextField;
	}

	/**
	 * This method initializes jButton
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(41, 237, 81, 28));
			jButton.setText("添加");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//封装数据到实体类
					Student student = new Student();
					student.setAddDate(new Date(System.currentTimeMillis()));
					student.setEmail(jEmailTextField1.getText());
					student.setPhone(jPhoneTextField1.getText());
					student.setSage(Integer.parseInt(jAgeTextfield.getText()));
					student.setSchoolname(jAddressComboBox.getSelectedItem()+"");
					student.setSname(jNameTextField.getText());

					if(manRadioButton.isSelected()){
						student.setSex(true);
					}else {
						student.setSex(false);
					}
					//实例dao  执行保存
					StudentDao dao = new StudentDao();
					boolean is = dao.addStudent(student);
					if(is){
						AddForm.this.setVisible(false);
						JOptionPane.showMessageDialog(AddForm.this, "添加成功！");
					}else{
						JOptionPane.showMessageDialog(AddForm.this, "添加失败！");
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jPasswordField
	 *
	 * @return javax.swing.JPasswordField
	 */
	private JTextField getJPasswordField() {
		if (jAgeTextfield == null) {
			jAgeTextfield = new JTextField();
			jAgeTextfield.setBounds(new Rectangle(131, 37, 100, 22));
		}
		return jAgeTextfield;
	}

	/**
	 * This method initializes jEmailTextField1
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getJEmailTextField1() {
		if (jEmailTextField1 == null) {
			jEmailTextField1 = new JTextField();
			jEmailTextField1.setBounds(new Rectangle(131, 78, 101, 20));
		}
		return jEmailTextField1;
	}

	/**
	 * This method initializes jPhoneTextField1
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getJPhoneTextField1() {
		if (jPhoneTextField1 == null) {
			jPhoneTextField1 = new JTextField();
			jPhoneTextField1.setBounds(new Rectangle(131, 114, 101, 26));
		}
		return jPhoneTextField1;
	}

	/**
	 * This method initializes jAddressComboBox
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJAddressComboBox() {
		if (jAddressComboBox == null) {
			jAddressComboBox = new JComboBox();

			SchoolDao dao = new SchoolDao();
			Vector<String> schoolnames = dao.querySchool();
			schoolnames.add(0, "-请选择-");

			jAddressComboBox.setModel(new DefaultComboBoxModel(schoolnames));

			jAddressComboBox.setBounds(new Rectangle(131, 160, 97, 21));
		}
		return jAddressComboBox;
	}

	/**
	 * This method initializes jButton1
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(160, 237, 89, 29));
			jButton1.setText("取　消");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
//					AddForm.this.setVisible(false);
					AddForm.this.dispose();
//					System.exit(0);
					System.out.println("取消"); // TODO Auto-generated Event
					// stub actionPerformed()
				}
			});
		}
		return jButton1;
	}

	public AddForm() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize() {
		this.setBounds(400, 300, 327, 316);
		this.setContentPane(getJContentPane());
		this.setTitle("添加学员");
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jAddressLabel1 = new JLabel();
			jAddressLabel1.setBounds(new Rectangle(41, 157, 61, 26));
			jAddressLabel1.setText("所属学校");
			jPhoneLabel1 = new JLabel();
			jPhoneLabel1.setBounds(new Rectangle(41, 114, 62, 27));
			jPhoneLabel1.setText("手机号码");
			jEmailLabel1 = new JLabel();
			jEmailLabel1.setBounds(new Rectangle(41, 77, 61, 23));
			jEmailLabel1.setText("电子邮箱");
			jUpassLabel1 = new JLabel();
			jUpassLabel1.setBounds(new Rectangle(41, 37, 57, 26));
			jUpassLabel1.setText("学员年龄");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(41, 7, 61, 23));
			jLabel.setText("学员姓名");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJNameTextField(), null);

			jContentPane.add(getJButton(), null);
			jContentPane.add(jUpassLabel1, null);
			jContentPane.add(getJPasswordField(), null);
			jContentPane.add(jEmailLabel1, null);
			jContentPane.add(getJEmailTextField1(), null);
			jContentPane.add(jPhoneLabel1, null);
			jContentPane.add(getJPhoneTextField1(), null);
			jContentPane.add(jAddressLabel1, null);
			jContentPane.add(getJAddressComboBox(), null);
			jContentPane.add(getJButton1(), null);

			final JLabel label = new JLabel();
			label.setText("性   别");
			label.setBounds(41, 199, 49, 18);
			jContentPane.add(label);

			manRadioButton = new JRadioButton();
			buttonGroup.add(manRadioButton);
			manRadioButton.addActionListener(new ActionListener() {
				public void actionPerformed(final ActionEvent e) {
				}
			});
			manRadioButton.setText("男");
			manRadioButton.setBounds(99, 195, 49, 26);
			jContentPane.add(manRadioButton);

			womanRadioButton = new JRadioButton();
			womanRadioButton.setText("女");
			buttonGroup.add(womanRadioButton);
			womanRadioButton.setBounds(154, 195, 62, 26);
			jContentPane.add(womanRadioButton);
		}
		return jContentPane;
	}

}
